
package cn.net.withub.dataCollector.web.utils;

import cn.net.withub.dataCollector.web.entity.Tree;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;

public class BaseTest {
    @Test
    public void wordTe() {
        List<Tree> list = new ArrayList<>();
        Tree tree;
        list.add(new Tree("1", "0", "IT"));
        list.add(new Tree("2", "1", "JAVA"));
        list.add(new Tree("3", "1", "GO"));
        list.add(new Tree("4", "1", "PHP"));
        list.add(new Tree("5", "2", "java1"));
        list.add(new Tree("6", "2", "java2"));
        list.add(new Tree("7", "3", "go1"));
        list.add(new Tree("7", "3", "go2"));
        list.add(new Tree("8", "3", "go3"));
        list.add(new Tree("9", "4", "php1"));
        System.out.println(list);
        JSONArray result = listToTree(JSONArray.parseArray(JSON.toJSONString(list)),"id","pid","children");
        System.out.println(JSON.toJSONString(result));

    }
    @Test
    public void testTree() {
        List<Map<String,Object>> data = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("id",1);
        map.put("pid",0);
        map.put("name","甘肃省");
        data.add(map);
        Map<String,Object> map2 = new HashMap<>();
        map2.put("id",2);
        map2.put("pid",1);
        map2.put("name","天水市");
        data.add(map2);
        Map<String,Object> map3 = new HashMap<>();
        map3.put("id",3);
        map3.put("pid",2);
        map3.put("name","秦州区");
        data.add(map3);
        Map<String,Object> map4 = new HashMap<>();
        map4.put("id",4);
        map4.put("pid",0);
        map4.put("name","北京市");
        data.add(map4);
        Map<String,Object> map5 = new HashMap<>();
        map5.put("id",5);
        map5.put("pid",4);
        map5.put("name","昌平区");
        data.add(map5);
        //JSONArray result = listToTree(JSONArray.parseArray(JSON.toJSONString(data)),"id","pid","children");
        JSONArray result = listToTree(JSONArray.parseArray(JSON.toJSONString(data)),"id","pid","children");
        System.out.println(JSON.toJSONString(result));

    }
    @Test
    public void testTree1() {
        List<Map<String,Object>> data = new ArrayList<>();
        Map<String,Object> ma = new HashMap<>();
        ma.put("id",1);
        ma.put("pid","0");
        ma.put("name","中国");
        data.add(ma);
        Map<String,Object> map = new HashMap<>();
        map.put("id",2);
        map.put("pid",1);
        map.put("name","重庆市");
        data.add(map);
        Map<String,Object> m = new HashMap<>();
        m.put("id",3);
        m.put("pid",1);
        m.put("name","四川省");
        data.add(m);
        Map<String,Object> map2 = new HashMap<>();
        map2.put("id",4);
        map2.put("pid",2);
        map2.put("name","綦江区");
        data.add(map2);
        Map<String,Object> map22 = new HashMap<>();
        map22.put("id",5);
        map22.put("pid",2);
        map22.put("name","沙坪坝区");
        data.add(map22);
        Map<String,Object> map3 = new HashMap<>();
        map3.put("id",6);
        map3.put("pid",3);
        map3.put("name","泸州市");
        data.add(map3);
        Map<String,Object> map4 = new HashMap<>();
        map4.put("id",7);
        map4.put("pid",3);
        map4.put("name","宜宾市");
        data.add(map4);
        //JSONArray result = listToTree(JSONArray.parseArray(JSON.toJSONString(data)),"id","pid","children");
        JSONArray result = listToTree(JSONArray.parseArray(JSON.toJSONString(data)),"id","pid","children");
        List<Map<String, Object>> result1 = listToTree1(data,"id","pid","children");
        System.out.println("-------------");
    }

    public static JSONArray listToTree(JSONArray arr, String id, String pid, String child) {
        JSONArray r = new JSONArray();
        JSONObject hash = new JSONObject();
        //将数组转为Object的形式，key为数组中的id
        for (int i = 0; i < arr.size(); i++) {
            JSONObject json = (JSONObject) arr.get(i);
            hash.put(json.getString(id), json);
        }
        //遍历结果集
        for (int j = 0; j < arr.size(); j++) {
            //单条记录
            JSONObject aVal = (JSONObject) arr.get(j);
            //在hash中取出key为单条记录中pid的值
            JSONObject hashVP = (JSONObject) hash.get(aVal.get(pid).toString());
            //如果记录的pid存在，则说明它有子节点，将她添加到孩子节点的集合中
            if (hashVP != null) {
                //检查是否有child属性
                if (hashVP.get(child) != null) {
                    JSONArray ch = (JSONArray) hashVP.get(child);
                    ch.add(aVal);
                    hashVP.put(child, ch);
                } else {
                    JSONArray ch = new JSONArray();
                    ch.add(aVal);
                    hashVP.put(child, ch);
                }
            } else {
                r.add(aVal);
            }
        }
        return r;
    }

    public static List<Map<String, Object>> listToTree1(List<Map<String, Object>> list, String id, String pid, String child) {
        List<Map<String, Object>> treeList = new ArrayList<>();
        String key = "";
        String pkey = "";
        //将数组转为Map的形式，key为数组中的id
        Map<String, Object> dataMap = new HashMap<>();
        for (Map<String, Object> map : list) {
            key = getStr(map.get(id));
            if (!"".equals(key)) {
                dataMap.put(key, map);
            }
        }
        System.out.println(dataMap);
        for (Map<String, Object> map : list) {
            //在dataMap中取出key为单条记录中pid的值
            pkey = getStr(map.get("pid"));
            if (!"".equals(pkey)) {
                Map<String, Object> m = (Map<String, Object>) dataMap.get(getStr(map.get(pid)));
                //如果记录的pid存在，则说明它有子节点，将她添加到孩子节点的集合中
                if (m != null) {
                    //检查是否有child属性
                    if (m.get(child) != null) {
                        List<Map<String, Object>> childList = (List<Map<String, Object>>) m.get(child);
                        childList.add(map);
                        m.put(child, childList);
                    }else {
                        List<Map<String, Object>> childList = new ArrayList<>();
                        childList.add(map);
                        m.put(child, childList);
                    }
                }else {
                    treeList.add(map);
                }
            }else {
                treeList.add(map);
            }
        }
        return treeList;
    }

    public static String getStr(Object obj) {
        return obj == null || "".equals(obj.toString()) ? "" : obj.toString();
    }
}
